package main

import (
	"encoding/json"
	"fmt"
	"reflect"
)

type IT struct {
	Company  string   `json:"company"`
	Subjects []string `json:"subjects"`
	IsOk     bool     `json:"IsOk,bool"`
	Price    float64  `json:",string"`
}

type User struct {
	Name string `json:"Name" bson:"b_name"`
	Age  int	`json:"age" bson:"b_age"`
}

func main() {
	jsonBuf := `{"company":"shanghai ky","IsOk":true,"Price":"66.66","subjects":["go","c++"]}`

	var tmp IT //定义一个结构体变量
	fmt.Println(tmp)
	err := json.Unmarshal([]byte(jsonBuf), &tmp)

	if err != nil {
		fmt.Println("err = ", err)
		return
	}
	fmt.Println("tmp = ", tmp)
	fmt.Printf("tmp = %+v\n", tmp)

	var u User
	h := `{"name":"张三","age":15}`
	err = json.Unmarshal([]byte(h), &u)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("u=%v\n", u)
		fmt.Printf("%T\n", u)
	}

	//通过反射获取字段tag的键值对
	t:=reflect.TypeOf(u)
	for i:=0;i<t.NumField();i++{
		sf:=t.Field(i)
		fmt.Println(sf.Tag.Get("json"),",",sf.Tag.Get("bson"))
	}

}
